/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.util.ArrayList;
import java.util.List;

import blueprint4j.utils.*;

import javax.swing.*;

public class DataPairFieldStringCombo extends DataPairDefault implements FieldBinding {
    private BindStringableInterface field;
    protected JComboBox combo_field=null;
    protected VectorUniqueString display = null;
    protected VectorString values = null;
    
    public DataPairFieldStringCombo(Binder binder,BindStringableInterface p_field,String [] display,String[] values,boolean editable){
        super(binder,p_field);
        this.display = new VectorUniqueString(display);
        this.values = new VectorString(values);
        combo_field=new JComboBox(display);
        combo_field.setEnabled(editable);
        field=p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(combo_field);
    }
    
    public DataPairFieldStringCombo(Binder binder,BindStringableInterface p_field,String[] values,boolean editable){
        this(binder,p_field,values,values,editable);
    }
    
    protected String getSelectedItem() {
        if (combo_field.getSelectedIndex() < 0) {
            return null;
        }
        return values.get(combo_field.getSelectedIndex());
    }
    
    private void setSelectedItem(int index) {
        if (index < 0 ) {
            combo_field.setSelectedItem(null);
        } else {
            combo_field.setSelectedIndex(index);
        }
    }
    
    public boolean isChanged(){
        String f=field.getAsString();
        if (f==null){
            return f!=getSelectedItem();
        } else {
            return !f.equals(getSelectedItem());
        }
    }
    
    public void intoControl(){
        if (field.getAsString() == null || field.getAsString().length() == 0) {
            setSelectedItem(-1);
        } else if (values.indexOf(field.getAsString())== -1) {
            combo_field.addItem(field.getAsString());
            display.add(field.getAsString());
            values.add(field.getAsString());
            setSelectedItem(values.indexOf(field.getAsString()));
        } else {
            setSelectedItem(values.indexOf(field.getAsString()));
        }
    }
    
    public void intoBindable(){
        field.setAsString(getSelectedItem());
    }
    
    public Bindable getBindable(){
        return field.getBindable();
    }
    
    public String getCurrentValue(){
        return (String)getSelectedItem();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindStringableInterface) p_field;
    }
    
    public void resetControl() {}
    
    public void setSelectedIndex(int index) {
        combo_field.setSelectedIndex(index);
    }
 
}

